package com.mymorningroutine;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.mymorningroutine.obj.Routine;
import android.app.*;
import android.widget.*;
import android.content.*;
import android.view.View.*;
import android.view.*;
import com.mymorningroutine.server.*;
import android.util.*;
import android.widget.AdapterView.*;
import com.mymorningroutine.utils.*;
import android.graphics.drawable.*;

public class RoutineDetailActivity extends Activity
{

	Routine routine;
	ListView listView;
	RoutineActivitiesAdapter listAdapter;
	
	private static final Integer RESULT_CODE = 2;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setDisplayShowHomeEnabled(true);
		
		
		
		
		setContentView(R.layout.activity_dialog_routine_activities);
		listView = (ListView) findViewById(android.R.id.list);
		
		Intent i = getIntent();
		Integer itemId = i.getIntExtra("routineId", 0);
		if(itemId == 0){
			finish();
		}
		routine = Singleton.getRoutineTable(getApplicationContext()).getRoutine(itemId);
		
		setTitle(routine.getTitle());
		listAdapter = new RoutineDetailActivitiesAdapter(getApplicationContext(), R.layout.list_routine_activity_detail, routine.getActivities()){

			@Override
			public boolean deleteActivityFromRoutine(int position)
			{
				// TODO: Implement this method
				
				routine.getActivities().remove(position);
				listAdapter.notifyDataSetChanged();
				listView.invalidateViews();
				
				return true;
			}


			@Override
			public void moveItem(Integer position, Integer positionNext)
			{
				RoutineDetailActivity.this.moveItem(position,positionNext);
			}
			
			
		};
		listView.setAdapter(listAdapter);
		
		
		
		
		
		
		/*
		listView.setLongClickable(true);
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> p1, final View p2, int p3, long p4)
				{
					final  com.mymorningroutine.obj.Activity activity = Singleton.getActivityTable(getApplicationContext()).getActivity(p2.getId());

					AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(RoutineDetailActivity.this);
// set title
					alertDialogBuilder. setTitle(getResources().getString(R.id.action_delete)) ;
// set dialog message
					alertDialogBuilder
						. setMessage(getResources().getString(R.id.action_delete) + " '" + activity.getTitle() + "' from routine '" + routine.getTitle() )
						. setCancelable(false)
						. setPositiveButton(getResources().getString(R.string.yes) , new DialogInterface. OnClickListener() {
							public void onClick(DialogInterface dialog, int id)
							{
// if this button is clicked, close current activity
								routine.getActivities().remove(listView.getPositionForView(p2));
								listAdapter.notifyDataSetChanged();
								
									}
						})
						. setNegativeButton(getResources().getString(R.string.no), new DialogInterface. OnClickListener() {
							public void onClick(DialogInterface dialog, int id)
							{
// if this button is clicked, just close
// the dialog box and do nothing
								dialog.cancel() ;
							}
						}) ;
// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
// show it
					alertDialog.show();


					return true;
	}
			
		});*/
		
	/*	Button addActivityButton = (Button) findViewById(R.id.button_add_activity_routine);
		Drawable icon = getResources().getDrawable(android.R.drawable.ic_menu_save);
		addActivityButton.setCompoundDrawables(icon, null, null, null);
		addActivityButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					Class<?> itemActivity = DialogActivities.class;
					Intent intent = new Intent(getApplicationContext(), itemActivity);
					intent.putExtra("routineId", routine.getId());
					startActivityForResult(intent, RESULT_CODE);
					
				}

			
		});
		*/
		Button saveRoutineButton = (Button) findViewById(R.id.button_save_routine);
		saveRoutineButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					RoutineTable routineTable = Singleton.getRoutineTable(getApplicationContext());
					boolean result = routineTable.updateRoutine(routine);
					if(result){
						finish();
					}
				}


			});
			
			
			
		Button cancelRoutineButton = (Button) findViewById(R.id.button_cancel);
		cancelRoutineButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
						finish();
				}


			});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem)
	{
		// TODO: Implement this method
		switch (menuItem.getItemId()) {
			case android.R.id.home:
				finish();
				break;
				
			
		}
		return super.onOptionsItemSelected(menuItem);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		getMenuInflater().inflate(R.menu.dialog_routine_activities,menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		// TODO: Implement this method
		switch(item.getItemId()){
			case R.id.action_delete_routine:
				AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(RoutineDetailActivity.this);
// set title
				alertDialogBuilder. setTitle(getResources().getString(R.string.activity_title)) ;
// set dialog message
				alertDialogBuilder
					. setCancelable(true)
					.setMessage(R.string.confirmation_delete_routine)
					. setPositiveButton(getResources().getString(R.string.ok) , new DialogInterface. OnClickListener() {
						public void onClick(DialogInterface dialog, int id)
						{
// if this button is clicked, close
// current activity
							//Delete activity
							Singleton.getRoutineTable(getApplicationContext()).deleteRoutine(routine.getId());
							finish();
						}
					})
					. setNegativeButton(getResources().getString(R.string.cancel) , new DialogInterface. OnClickListener() {
						public void onClick(DialogInterface dialog, int id)
						{
// if this button is clicked, just close
// the dialog box and do nothing
							dialog.cancel() ;
						}
					}) ;
// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
// show it
				alertDialog.show();

				break;
			case R.id.action_add_activity_routine:
				Class<?> itemActivity = DialogActivities.class;
				Intent intent = new Intent(getApplicationContext(), itemActivity);
				intent.putExtra("routineId", routine.getId());
				startActivityForResult(intent, RESULT_CODE);
				break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode,resultCode,data);
		if ((requestCode == RESULT_CODE ) && (data != null)){
			Integer activityId = data.getIntExtra("activityId", 0);
			Log.e(getClass().toString(), "Returned activity id = " + activityId);
			if(activityId != 0){
				com.mymorningroutine.obj.Activity act = Singleton.getActivityTable(getApplicationContext()).getActivity(activityId);
				routine.getActivities().add(act);
				listAdapter.notifyDataSetChanged();
			} 
		}
		}
		
		
	public void moveItem(int position, int positionNext)
	{
		if ((position < positionNext && position < routine.getActivities().size() - 1) ||
			(position > positionNext && position >= 1))
		{
			com.mymorningroutine.obj.Activity         act = routine.getActivities().get(position);
			routine.getActivities().set(position, routine.getActivities().get(positionNext));
			routine.getActivities().set(positionNext, act);
			RoutineActivitiesAdapter rta = (RoutineActivitiesAdapter) listView.getAdapter();
			 rta.notifyDataSetChanged();
			listView.invalidateViews();
			listView.refreshDrawableState();
		 listView.setAdapter(rta);
		}
	}

}
